package com.scitc.pleasurevideo;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scitc.pleasurevideo.base.ETActivity;
import com.scitc.pleasurevideo.data.DataUtil;
import com.scitc.pleasurevideo.data.ETMan;
import com.scitc.pleasurevideo.helper.ForwardHelper;
import com.scitc.pleasurevideo.okgoutil.HttpCompleteOrFail;
import com.scitc.pleasurevideo.okgoutil.OkGoUtil;
import com.scitc.pleasurevideo.util.ConstUtil;
import com.scitc.pleasurevideo.util.TimeUtil;
import com.scitc.pleasurevideo.x5webview.MyApplication;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author 853
 */
public class MainActivity extends ETActivity {
    private int initTime = 3;
    private boolean isStartApp = true;
    private TextView timeText;
    private Timer time = new Timer();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                timeText.setText(initTime + "s");
                initTime--;
            }
        }
    };
    private RelativeLayout container;
    private ImageView iconimage;
    private LinearLayout jumpBtn;
    private TextView jumpText;
    private RelativeLayout bottomBar;
    private ImageView logo;
    private TextView logoTitle;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setSwipeBackEnable(false);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }

        context = this;

        initView();

        okGO();

        onClick();

        checkStatus();


        //3秒后自动进入程序
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                if (context != null) {
                    if (isStartApp) {
                        ForwardHelper.toMain(mInstance);
                        finish();
                    } else {
                        finish();
                    }
                }

            }
        }, 3100);

        //每隔一秒修改显示倒计时
        time.schedule(new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();
                message.what = 0;
                handler.sendMessage(message);
            }
        }, 4, 1000);
    }

    /**
     * 检查日期的
     */
    private void checkStatus() {
        String date = TimeUtil.getDate(System.currentTimeMillis());
        //判断这是不是新的一天
        if (!date.equals(ETMan.getPref(MyApplication.getInstance(), ConstUtil.TODAY_DATE, ""))) {
            //新的一天--重置播放时长
            ETMan.savePref(MyApplication.getInstance(), ConstUtil.APP_PLAY_TIME_SECOND_TODAY,0);
        }
        ETMan.savePref(MyApplication.getInstance(), ConstUtil.TODAY_DATE,date);
    }

    private void onClick() {
        //点击跳过广告时间，直接进入程序
        jumpBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isStartApp) {
                    ForwardHelper.toMain(mInstance);
                    context = null;
                    finish();
                } else {
                    finish();
                }
            }
        });

    }

    /**
     * 获取服务器返回数据 是否允许打开程序
     */
    private void okGO() {
        //http://119.23.249.61/programBackstage/isStartApp?name=pleasurevideo&version=1.4
//        String version = DataUtil.getVersionName(this);
//        OkGo.<String>get("http://119.23.249.61/programBackstage/isStartApp?version=" + version + "&name=pleasurevideo")
//                .tag(mInstance)
//                .execute(new StringCallback() {
//                    @Override
//                    public void onSuccess(Response<String> response) {
//                        if ("true".equals(response.body())) {
//                            isStartApp = true;
//                        } else {
//                            isStartApp = false;
//                        }
//                    }
//                });

        OkGoUtil.init().isAppStart(new HttpCompleteOrFail() {
            @Override
            public void onComplete(String json) {
                if ("true".equals(json)) {
                    isStartApp = true;
                } else {
                    isStartApp = false;
                }
            }

            @Override
            public void onFile(String information) {
                isStartApp = false;
            }
        });
    }

    private void initView() {
        timeText = (TextView) findViewById(R.id.timeText);
        container = (RelativeLayout) findViewById(R.id.container);
        iconimage = (ImageView) findViewById(R.id.iconimage);
        jumpBtn = (LinearLayout) findViewById(R.id.jumpBtn);
        jumpText = (TextView) findViewById(R.id.jumpText);
        bottomBar = (RelativeLayout) findViewById(R.id.bottomBar);
        logo = (ImageView) findViewById(R.id.logo);
        logoTitle = (TextView) findViewById(R.id.logoTitle);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }
}
